package cn.com.twoke.app.pin_dou_dou.color;


import java.awt.Color;

public class ColorDifferenceAnalyzer {

    public static final ColorDifferenceCalculator CIE76 = ColorDifferenceCalculator.CIE76();
    public static final ColorDifferenceCalculator CIE94 = ColorDifferenceCalculator.CIE94();
    public static final ColorDifferenceCalculator CIE2000 = ColorDifferenceCalculator.CIE2000();
    public static final ColorDifferenceCalculator CIE_CMC = ColorDifferenceCalculator.CIE_CMC();

    /**
     * 根据 ΔE 值判断色差感知级别
     */
    public static String getPerceptionLevel(double deltaE) {
        if (deltaE < Threshold.VERY_STRICT) {
            return "不可察觉";
        } else if (deltaE < Threshold.STRICT) {
            return "专家可察觉";
        } else if (deltaE < Threshold.NORMAL) {
            return "经验观察者可察觉";
        } else if (deltaE < Threshold.LOOSE) {
            return "一般人可察觉";
        } else if (deltaE < 10.0) {
            return "明显差异";
        } else if (deltaE < 20.0) {
            return "很大差异";
        } else {
            return "完全不同";
        }
    }
    
    /**
     * 判断两个颜色是否在视觉上相似
     */
    public static boolean isVisuallySimilar(Color color1, Color color2, double threshold) {
        double deltaE = CIE2000.deltaE(color1, color2);
        return deltaE < threshold;
    }
    
    /**
     * 推荐的相似性阈值
     */
    public static class Threshold {
        public static final double VERY_STRICT = 1.0;    // 专业应用
        public static final double STRICT = 2.0;         // 高质量要求
        public static final double NORMAL = 3.5;         // 一般应用
        public static final double LOOSE = 5.0;          // 宽松要求
    }
    
    /**
     * 比较多种色差计算方法
     */
    public static void compareAllMethods(Color color1, Color color2) {
        double de76 = CIE76.deltaE(color1, color2);
        double de94 = CIE94.deltaE(color1, color2);
        double de00 = CIE2000.deltaE(color1, color2);
        double deCMC = CIE_CMC.deltaE(color1, color2);
        
        System.out.printf("颜色1: RGB(%d,%d,%d)%n", 
            color1.getRed(), color1.getGreen(), color1.getBlue());
        System.out.printf("颜色2: RGB(%d,%d,%d)%n", 
            color2.getRed(), color2.getGreen(), color2.getBlue());
        System.out.printf("ΔE*76: %.3f (%s)%n", de76, getPerceptionLevel(de76));
        System.out.printf("ΔE*94: %.3f (%s)%n", de94, getPerceptionLevel(de94));
        System.out.printf("ΔE*00: %.3f (%s)%n", de00, getPerceptionLevel(de00));
        System.out.printf("ΔE-CMC: %.3f (%s)%n", deCMC, getPerceptionLevel(deCMC));
        System.out.println();
    }
}